package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.order.models.service.IOrderInfoService;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.shop.ShopMemberRelationMapper;
import com.ztgf.shopping.models.bo.shop.ShopMemberRelationBO;
import com.ztgf.shopping.models.bo.shop.ShopMemberRelationSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopMemberRelationDTO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.service.shop.IShopMemberRelationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.models.vo.shop.ShopMemberRelationVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 会员表  服务实现类
 * </p>
 *
 * @author zhanghualang
 * @since 2020-05-26
 */
@DubboService
public class IShopMemberRelationServiceImpl extends ServiceImpl<ShopMemberRelationMapper, ShopMemberRelationDTO> implements IShopMemberRelationService {
    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    private IShopInfoService iShopInfoService;

    @DubboReference
    private IOrderInfoService iOrderInfoService;

    @Override
    public boolean add(ShopMemberRelationSaveBO bo) {
        //验证是否是被逻辑删除的 状态 0正常，1删除。默认0
        ShopMemberRelationDTO oldDto = getByShopId(bo.getSellShopId(), bo.getBuyShopId(), 1);
        if (oldDto != null){
            //若是被逻辑删除的，状态改为正常
            ShopMemberRelationDTO dto = new ShopMemberRelationDTO();
            dto.setId(oldDto.getId());
            dto.setStatus(0);
            return this.updateById(dto);
        }else {
            ShopMemberRelationDTO shopMemberRelationDTO = new ShopMemberRelationDTO();
            BeanUtils.copyProperties(bo,shopMemberRelationDTO);
            Long id = ymlConfig.getPkId();
            shopMemberRelationDTO.setId(id);
            return this.insert(shopMemberRelationDTO);
        }
    }

    @Override
    public Page<ShopMemberRelationVO> queryMemberPageList(ShopMemberRelationBO shopMemberRelationBO) {
        Page<ShopMemberRelationVO> page = new Page<>(shopMemberRelationBO.getPageNo(), shopMemberRelationBO.getPageSize());

        List<ShopMemberRelationVO> shopMemberRelationVOList = baseMapper.queryMemberPageList(page, shopMemberRelationBO);
        for (ShopMemberRelationVO shopMemberRelationVO : shopMemberRelationVOList){
            shopMemberRelationVO.setMoney(iOrderInfoService.getBuyShopConsumption(shopMemberRelationVO.getBuyShopId(), shopMemberRelationVO.getSellShopId()));
        }
        return page.setRecords(shopMemberRelationVOList);
    }

    @Override
    public ShopMemberRelationDTO getByShopId(Long sellShopId, Long buyShopId, Integer status) {
        Wrapper<ShopMemberRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("sell_shop_id", sellShopId).eq("buy_shop_id", buyShopId);
        wrapper.eq("status", status);//状态 0正常，1删除。默认0

        return this.selectOne(wrapper);
    }

    @Override
    public boolean updateByShopId(Long sellShopId, Long buyShopId, Integer status) {
        Wrapper<ShopMemberRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("sell_shop_id", sellShopId).eq("buy_shop_id", buyShopId);
        wrapper.eq("status", status);//状态 0正常，1删除。默认0

        ShopMemberRelationDTO dto = new ShopMemberRelationDTO();
        dto.setBuyShopId(buyShopId);
        dto.setSellShopId(sellShopId);
        dto.setStatus(status);

        return this.update(dto, wrapper);
    }
}
